#include "ccgamewnd.h"
#include "qevent.h"
#include <QMessageBox>
#include <QPainter>

CCGameWnd::CCGameWnd(QWidget *parent)
    : QFrame{parent}
{
    mRed = true;           //下一颗所下棋子的颜色，初始黑棋

    mClient = new Client;    //创建客户端
    connect(mClient, &Client::sigOtherUserMoveCC, this, &CCGameWnd::slotOtherUserMove);    //客户端的其他玩家行动信号连接游戏窗口的槽函数
    connect(mClient, &Client::sigNewGame, this, &CCGameWnd::slotNewGame);                //客户端的新游戏信号连接游戏窗口的槽函数

    //转发信号，给CCGameWnd处理
    connect(mClient, &Client::sigOutput,this, &CCGameWnd::sigOutput);                   //客户端的TextEdit输出text信号连接游戏窗口的信号
    connect(mClient, &Client::sigUpdateRoomState,this, &CCGameWnd::sigUpdateRoomState); //客户端的更新房间状态信号连接游戏窗口的信号

    //根据棋盘图片确定棋盘大小
    QPixmap pixChessBoard(chessBoardPath);
    setFixedSize(pixChessBoard.size());
    //根据棋子图片确定棋子大小
    mItemImage = QPixmap(itemPath);
    mItemWidth = mItemImage.width() / 2;
    mItemHeight = mItemImage.height() / 7;

    slotNewGame();          //新游戏
    startTimer(500);		//启动显隐选中元素的定时器
}

void CCGameWnd::Connect(QString ip, int port, int room)
{
    mClient->ConnectToServer(ip, port, room);
}

void CCGameWnd::Say(QString text)
{
    mClient->SendMsg_Say(text);  //TCP通信
}

void CCGameWnd::Output(QString text)
{
     emit sigOutput(text);        //发送TextEdit输出text信号（主窗口接收）
}

void CCGameWnd::GetRoomState(QList<ROOM_STATE> &state)
{
    mClient->GetRoomState(state);
}

void CCGameWnd::slotOtherUserMove(int x1, int y1, int x2, int y2)
{
    QPoint mSI(x1, y1);
    QPoint pt(x2, y2);
    bool bDeleteSHUAI = false;
    DeleteItemAtPoint(pt, bDeleteSHUAI);
    for (int i = 0; i < mItems.size(); i++) {
        if (mItems[i].mPt == mSI) {
            mSelectedItem = mItems[i];
        }
    }
    ChangeItemPoint(mSelectedItem.mPt, pt);
    //胜利判断
    if (bDeleteSHUAI) {
        QString str = mRed ? QStringLiteral("红方胜利！"):QStringLiteral("黑方胜利！");
        QMessageBox::information(nullptr, "游戏结束", str, QMessageBox::Yes, QMessageBox::Yes);
        slotNewGame();
        return ;
    }
    mIsExistSelect = false;
    mRed = !mRed;
    update();
}



void CCGameWnd::slotNewGame()
{
    InitItems();
    mSelectedItem.mPt = QPoint(-1,-1);
    mRed = true;             //当前该红旗下
    mIsExistSelect = false;  //当前不存在选中的棋子
}

void CCGameWnd::paintEvent(QPaintEvent *)
{
    DrawChessboard();
    update();
    for (int i = 0; i < mItems.size(); i++) {
        DrawItems(mItems[i]);
    }
    update();
}

void CCGameWnd::mousePressEvent(QMouseEvent* e)
{
    if (mClient->IsBlack()) {  //客户端返回红棋
        if (!mRed) {           //但接下来应该由黑棋下
            return;
        }
    }
    else {                     //客户端返回黑棋
        if (mRed) {            //但接下来应该由红棋下
            return;
        }
    }

    //获得鼠标点所对应的棋盘点pt
    QPoint pt;
    pt.setX((e->pos().x() - startX) / rectWidth);
    pt.setY((e->pos().y() - startY) / rectHeight);

    //是否有选中的棋子
    if (mIsExistSelect) {
        //已存在棋子，判断鼠标点击的是否是选中棋子
        if (pt == mSelectedItem.mPt) {
            //再次点击已经选择的棋子，什么也不做
            return;
        }
        //点击其它棋子
        CCItem ClickedItem;
        if (FindItemAtPoint(pt, ClickedItem)) {
            //点击的同色的另外一个棋子，改选
            if ((mRed && ClickedItem.mRed == 0) || (!mRed && ClickedItem.mRed != 0)) {
                SetItemShow(mSelectedItem, true);
                mSelectedItem = ClickedItem;
                return;
            }
        }
        //点击的异色棋子，判断是否能走能吃
        QVector<QPoint> moveArea;
        GetMoveArea(mSelectedItem, moveArea);		//获取已选择棋子的可移动区域
        if (moveArea.contains(pt)) {
             mClient->SendMsg_MoveCC(mSelectedItem.mPt.x(), mSelectedItem.mPt.y(), pt.x(), pt.y());  //执行行动落子
            //包含当前鼠标点中的棋子，则能吃
            bool bDeleteSHUAI = false;
            DeleteItemAtPoint(pt, bDeleteSHUAI);
            ChangeItemPoint(mSelectedItem.mPt, pt);

            if (bDeleteSHUAI) {
                if (mRed == mClient->IsBlack()) {
                    //赢的一方通知服务器自己赢了
                    mClient->SendMsg_Success();
                }
                QString str = mRed ? QStringLiteral("红方胜利！"):QStringLiteral("黑方胜利！");
                QMessageBox::information(nullptr, "游戏结束 ", str , QMessageBox::Yes, QMessageBox::Yes);
                slotNewGame();
                return ;
            }
            mIsExistSelect = false;
            mRed = !mRed;
            update();
            return ;
        }
        else {
            //不能走到该位置
            return ;
        }
    }
    else {
        //当前没有选中棋子
        CCItem ClickedItem;
        if (FindItemAtPoint(pt, ClickedItem)) {
            //如果点中一个棋子，是当前走棋方的颜色，就选中了
            if ((mRed && ClickedItem.mRed == 0) || (!mRed && ClickedItem.mRed == 1)) {
                mSelectedItem = ClickedItem;
                mIsExistSelect = true;
                return;
            }
        }
    }
}

void CCGameWnd::timerEvent(QTimerEvent *)
{
    for (int i = 0; i<mItems.size(); i++) {
        if (mItems[i].mPt == mSelectedItem.mPt) {
            mItems[i].mShow = !mItems[i].mShow;
        }
    }
}

void CCGameWnd::InitItems()
{
    mItems.clear();

    // 初始化所有黑方棋子
    CCItem item1(ITEM_JU, 1, QPoint(0, 0));
    CCItem item2(ITEM_MA, 1, QPoint(1, 0));
    CCItem item3(ITEM_XIANG, 1, QPoint(2, 0));
    CCItem item4(ITEM_SHI, 1, QPoint(3, 0));
    CCItem item5(ITEM_SHUAI, 1, QPoint(4, 0));
    CCItem item6(ITEM_SHI, 1, QPoint(5, 0));
    CCItem item7(ITEM_XIANG, 1, QPoint(6, 0));
    CCItem item8(ITEM_MA, 1, QPoint(7, 0));
    CCItem item9(ITEM_JU, 1, QPoint(8, 0));
    CCItem item10(ITEM_PAO, 1, QPoint(1, 2));
    CCItem item11(ITEM_PAO, 1, QPoint(7, 2));
    CCItem item12(ITEM_BING, 1, QPoint(0, 3));
    CCItem item13(ITEM_BING, 1, QPoint(2, 3));
    CCItem item14(ITEM_BING, 1, QPoint(4, 3));
    CCItem item15(ITEM_BING, 1, QPoint(6, 3));
    CCItem item16(ITEM_BING, 1, QPoint(8, 3));

    // 初始化所有红方棋子
    CCItem item_1(ITEM_JU, 0, QPoint(0, 9));
    CCItem item_2(ITEM_MA, 0, QPoint(1, 9));
    CCItem item_3(ITEM_XIANG, 0, QPoint(2, 9));
    CCItem item_4(ITEM_SHI, 0, QPoint(3, 9));
    CCItem item_5(ITEM_SHUAI, 0, QPoint(4, 9));
    CCItem item_6(ITEM_SHI, 0, QPoint(5, 9));
    CCItem item_7(ITEM_XIANG, 0, QPoint(6, 9));
    CCItem item_8(ITEM_MA, 0, QPoint(7, 9));
    CCItem item_9(ITEM_JU, 0, QPoint(8, 9));
    CCItem item_10(ITEM_PAO, 0, QPoint(1, 7));
    CCItem item_11(ITEM_PAO, 0, QPoint(7, 7));
    CCItem item_12(ITEM_BING, 0, QPoint(0, 6));
    CCItem item_13(ITEM_BING, 0, QPoint(2, 6));
    CCItem item_14(ITEM_BING, 0, QPoint(4, 6));
    CCItem item_15(ITEM_BING, 0, QPoint(6, 6));
    CCItem item_16(ITEM_BING, 0, QPoint(8, 6));

    mItems.push_back(item1);
    mItems.push_back(item2);
    mItems.push_back(item3);
    mItems.push_back(item4);
    mItems.push_back(item5);
    mItems.push_back(item6);
    mItems.push_back(item7);
    mItems.push_back(item8);
    mItems.push_back(item9);
    mItems.push_back(item10);
    mItems.push_back(item11);
    mItems.push_back(item12);
    mItems.push_back(item13);
    mItems.push_back(item14);
    mItems.push_back(item15);
    mItems.push_back(item16);

    mItems.push_back(item_1);
    mItems.push_back(item_2);
    mItems.push_back(item_3);
    mItems.push_back(item_4);
    mItems.push_back(item_5);
    mItems.push_back(item_6);
    mItems.push_back(item_7);
    mItems.push_back(item_8);
    mItems.push_back(item_9);
    mItems.push_back(item_10);
    mItems.push_back(item_11);
    mItems.push_back(item_12);
    mItems.push_back(item_13);
    mItems.push_back(item_14);
    mItems.push_back(item_15);
    mItems.push_back(item_16);
}

void CCGameWnd::DrawChessboard()
{
    QPainter painter(this);
    QPixmap pixChessBoard(chessBoardPath);
    painter.drawPixmap(0, 0, pixChessBoard);
}

void CCGameWnd::DrawItems(CCItem item)
{
    if (!item.mShow) {
        return;
    }
    QPainter painter(this);
    //图片上的矩形位置
    QRect rcSrc(mItemWidth * item.mRed,    //黑棋都在第二列，x坐标1，红棋都在第一列，x坐标0
                mItemHeight * item.mType,    //y坐标
                mItemWidth, mItemHeight);    //宽度和高度
    //画的位置
    QRect rcTarget(startX + item.mPt.x() * rectWidth,  //根据item的mPt坐标，以及（0,0）与棋盘左上角偏移量
                   startY + item.mPt.y() * rectHeight,
                   rectWidth,rectHeight);              //宽度和高度

    painter.drawPixmap(rcTarget, mItemImage, rcSrc);
}

void CCGameWnd::GetMoveArea(CCItem item, QVector<QPoint> &area)
{
    switch (item.mType) {
    case ITEM_JU:
    {
        GetMoveArea_JU(item, area);
        break;
    }
    case ITEM_MA:
    {
        GetMoveArea_MA(item, area);
        break;
    }
    case ITEM_XIANG:
    {
        GetMoveArea_XIANG(item, area);
        break;
    }
    case ITEM_SHI:
    {
        GetMoveArea_SHI(item, area);
        break;
    }
    case ITEM_SHUAI:
    {
        GetMoveArea_SHUAI(item, area);
        break;
    }
    case ITEM_PAO:
    {
        GetMoveArea_PAO(item, area);
        break;
    }
    case ITEM_BING:
    {
        GetMoveArea_BING(item, area);
        break;
    }
    }
}

void CCGameWnd::GetMoveArea_JU(CCItem item, QVector<QPoint>& moveArea)
{
    //棋子“车”的计算可移动区域算法简介：
    //1，向4个方向上依次遍历点，把空点都加入。
    //当碰到棋子时停止，如果是同色棋子，不加入；是异色棋子，则加入（该处认为可走，因为可以吃子）
    //左边
    int x = item.mPt.x() - 1;
    while(x >= 0) {
        QPoint ptNew = QPoint(x,item.mPt.y());
        CCItem item2;
        if (FindItemAtPoint(ptNew ,item2)) {
            if (item.mRed != item2.mRed) {
                moveArea.append(ptNew);
            }
            break;
        }
        moveArea.append(ptNew);
        x--;
    }
    //右边
    x = item.mPt.x() + 1;
    while(x <= 8) {
        QPoint ptNew = QPoint(x,item.mPt.y());
        CCItem item2;
        if (FindItemAtPoint(ptNew ,item2)) {
            if (item.mRed != item2.mRed) {
                moveArea.append(ptNew);
            }
            break;
        }
        moveArea.append(ptNew);
        x++;
    }

    //上边
    int y = item.mPt.y() - 1;
    while(y >= 0) {
        QPoint ptNew = QPoint(item.mPt.x(),y);
        CCItem item2;
        if (FindItemAtPoint(ptNew ,item2)) {
            if (item.mRed != item2.mRed) {
                moveArea.append(ptNew);
            }
            break;
        }
        moveArea.append(ptNew);
        y--;
    }
    //下方
    y = item.mPt.y() + 1;
    while(y <= 9) {
        QPoint ptNew = QPoint(item.mPt.x(),y);
        CCItem item2;
        if (FindItemAtPoint(ptNew ,item2)) {
            if (item.mRed != item2.mRed) {
                moveArea.append(ptNew);
            }
            break;
        }
        moveArea.append(ptNew);
        y++;
    }
}

void CCGameWnd::GetMoveArea_MA(CCItem item, QVector<QPoint>& moveArea )
{
    //棋子“马”的计算可移动区域算法简介：
    //1，求出8个待选位置，8个位置的偏移是（-2，-1）（-2，1）（2，-1）（2，1）（1，-2）（1，2）（-1，-2）（-1，2）存在关系：|x|+|y|=3
    //2，判断待选位置是否在棋盘内
    //3，判断中间是否有卡位的棋子
    //4，位置上是否已存在同色棋子
    CCItem item2;
    for (int i = -2; i <= 2; i++) {
        for(int j = -2; j <= 2; j++) {
            if (qAbs(i) + qAbs(j) == 3) {
                QPoint ptNew = item.mPt + QPoint(i, j);
                if (ptNew.x() >= 0 && ptNew.x() <= 8 && ptNew.y()>=0 && ptNew.y() <= 9) {

                }
                else {
                    continue;
                }

                //求该方向行走路线的 卡位元素位置
                QPoint ptDirect(0, 0);
                if (qAbs(i) > qAbs(j)) {
                    if (i > 0) {
                        ptDirect = QPoint(1, 0);
                    }
                    else {
                        ptDirect = QPoint(-1, 0);
                    }
                }
                else {
                    if (j > 0) {
                        ptDirect = QPoint(0, 1);
                    }
                    else {
                        ptDirect = QPoint(0, -1);
                    }
                }
                QPoint ptHit = item.mPt + ptDirect;	//马的卡位元素位置
                if (FindItemAtPoint(ptHit,item2)) {
                    //卡位
                    continue;
                }
                if (FindItemAtPoint(ptNew ,item2) && item.mRed == item2.mRed) {
                    //有本组item
                    continue;
                }
                moveArea.append(ptNew);
            }
        }
    }
}

void CCGameWnd::GetMoveArea_XIANG(CCItem item, QVector<QPoint>& moveArea )
{
    //棋子“相”的计算可移动区域算法简介：
    //1，求出4个待选位置
    //2，判断是否在该“相”对应的半边棋盘内
    //3，判断中间是否有卡位的棋子
    //4，位置上是否已存在同色棋子
    QVector<QPoint> directions;
    directions.push_back(QPoint(-2, -2));
    directions.push_back(QPoint(2, -2));
    directions.push_back(QPoint(2, 2));
    directions.push_back(QPoint(-2, 2));

    for (int i = 0; i < directions.size(); i++) {
        QPoint ptNew = item.mPt + directions[i];									//移动后的点
        if (item.mPt.y() <= 4) {														//上方的
            if (ptNew.x() >= 0 && ptNew.x() <= 8 && ptNew.y() >= 0 && ptNew.y() <= 4)	{	//移动后需仍在上半棋盘

            }
            else {
                continue;
            }
        }
        else {
            if (ptNew.x() >= 0 && ptNew.x() <= 8 && ptNew.y() >= 5 && ptNew.y() <= 9) {	//移动后需仍在下半棋盘

            }
            else {
                continue;
            }
        }

        QPoint ptHit = item.mPt + QPoint(directions[i].x() / 2,directions[i].y() / 2);		//卡位点在中点
        CCItem item2;
        if (FindItemAtPoint(ptHit, item2)) {
            //存在卡位棋子
            continue;
        }
        if (FindItemAtPoint(ptNew, item2) && item.mRed == item2.mRed) {
            //终点是同色的棋子
            continue;
        }
        moveArea.append(ptNew);
    }
}

void CCGameWnd::GetMoveArea_SHI(CCItem item, QVector<QPoint>& moveArea )
{
    //移动算法与相类似
    //4个待选的位置偏移
    QVector<QPoint> directions;
    directions.push_back(QPoint(-1, -1));
    directions.push_back(QPoint(1, -1));
    directions.push_back(QPoint(1, 1));
    directions.push_back(QPoint(-1, 1));

    for (int i = 0; i < directions.size(); i++) {
        QPoint ptNew = item.mPt + directions[i];									//移动后的点
        if (item.mPt.y() <= 4) {														//上方的
            if (ptNew.x() >= 3 && ptNew.x() <= 5 && ptNew.y() >= 0 && ptNew.y() <= 2) {		//移动后在上棋盘的(3,0)~(5,2)田字内

            }
            else {
                continue;
            }
        }
        else {
            if (ptNew.x() >= 3 && ptNew.x() <= 5 && ptNew.y() >= 7 && ptNew.y() <= 9)	{	//移动后需仍在下半棋盘

            }
            else {
                continue;
            }
        }

        //QPoint ptHit = item.m_pt + QPoint(directions[i].x()/2,directions[i].y()/2);		//卡位点在中点
        CCItem item2;
        //if (FindItemAtPoint(ptHit,item2))
        //{
        //	//存在卡位的棋子
        //	continue;
        //}
        if (FindItemAtPoint(ptNew, item2) && item.mRed == item2.mRed) {
            //终点是同色的棋子
            continue;
        }
        moveArea.append(ptNew);
    }
}

void CCGameWnd::GetMoveArea_SHUAI(CCItem item, QVector<QPoint>& moveArea)
{
    QVector<QPoint> directions;
    directions.push_back(QPoint(-1, 0));
    directions.push_back(QPoint(1, 0));
    directions.push_back(QPoint(0, 1));
    directions.push_back(QPoint(0, -1));

    //对将的情况没写

    for (int i = 0; i<directions.size(); i++) {
        QPoint ptNew = item.mPt + directions[i];									//移动后的点
        if (item.mPt.y() <= 4) {														//上方的
            if (ptNew.x() >= 3 && ptNew.x() <= 5 && ptNew.y() >= 0 && ptNew.y() <= 2)	{	//移动后在上棋盘的(3,0)~(5,2)田字内

            }
            else {
                continue;
            }
        }
        else {
            if (ptNew.x() >= 3 && ptNew.x() <= 5 && ptNew.y() >= 7 && ptNew.y() <= 9)	{	//移动后需仍在下半棋盘

            }
            else {
                continue;
            }
        }
        CCItem item2;
        if (FindItemAtPoint(ptNew ,item2) && item.mRed == item2.mRed) {
            //终点是同色的棋子
            continue;
        }
        moveArea.append(ptNew);
    }
}

void CCGameWnd::GetMoveArea_PAO(CCItem item, QVector<QPoint>& moveArea)
{
    //炮的可移动区域分为两部分：第一部分同“车”，第二部分为直线上隔一个棋子后的异色棋子
    //QVector<QPoint> juMoveArea;
    //GetMoveArea_JU(item,moveArea);

    //左边
    bool bFind = false;
    int x = item.mPt.x() - 1;
    while(x >= 0) {
        QPoint ptNew = QPoint(x,item.mPt.y());
        CCItem item2;
        if (FindItemAtPoint(ptNew, item2)) {
            if (bFind) {
                //如果之前找到过一个点，那么这是第2个点
                if(item.mRed != item2.mRed) {
                    //如果这第2个点是异色的，那么是可以吃的
                    moveArea.append(ptNew);
                }
                break;
            }
            bFind = true;
            x--;
            continue;
        }
        if (!bFind) {
            moveArea.append(ptNew);
        }
        x--;
    }
    //右边
    bFind = false;
    x = item.mPt.x() + 1;
    while(x<=8) {
        QPoint ptNew = QPoint(x, item.mPt.y());
        CCItem item2;
        if (FindItemAtPoint(ptNew ,item2)) {
            if (bFind) {
                //如果之前找到过一个点，那么这是第2个点
                if (item.mRed != item2.mRed) {
                    //如果这第2个点是异色的，那么是可以吃的
                    moveArea.append(ptNew);
                }
                break;
            }
            bFind = true;
            x++;
            continue;
        }
        if (!bFind) {
            moveArea.append(ptNew);
        }
        x++;
    }

    //上边
    bFind = false;
    int y = item.mPt.y() - 1;
    while(y >= 0)
    {
        QPoint ptNew = QPoint(item.mPt.x(), y);
        CCItem item2;
        if (FindItemAtPoint(ptNew ,item2)) {
            if (bFind) {
                //如果之前找到过一个点，那么这是第2个点
                if(item.mRed != item2.mRed)
                {
                    //如果这第2个点是异色的，那么是可以吃的
                    moveArea.append(ptNew);
                }
                break;
            }
            bFind = true;
            y--;
            continue;
        }
        if (!bFind) {
            moveArea.append(ptNew);
        }
        y--;

    }
    //下方
    bFind = false;
    y = item.mPt.y() + 1;
    while(y <= 9) {
        QPoint ptNew = QPoint(item.mPt.x(),y);
        CCItem item2;
        if (FindItemAtPoint(ptNew, item2)) {
            if (bFind) {
                //如果之前找到过一个点，那么这是第2个点
                if (item.mRed != item2.mRed) {
                    //如果这第2个点是异色的，那么是可以吃的
                    moveArea.append(ptNew);
                }
                break;
            }
            bFind = true;
            y++;
            continue;
        }
        if (!bFind) {
            moveArea.append(ptNew);
        }
        y++;

    }
}

void CCGameWnd::GetMoveArea_BING(CCItem item, QVector<QPoint>& moveArea)
{
    QVector<QPoint> directions;

    if (item.mRed == 0) {
        directions.push_back(QPoint(0, -1));	//红色上移
        if (item.mPt.y() < 5 ) {			//红色在下方，y<5就是已经过河，可以横向移动
            directions.push_back(QPoint(-1, 0));
            directions.push_back(QPoint(1, 0));
        }
    }
    else {
        directions.push_back(QPoint(0, 1));	//黑色下移
        if (item.mPt.y() >= 5 )	{		//黑色在上方，y>=5就是已经过河，可以横向移动
            directions.push_back(QPoint(-1, 0));
            directions.push_back(QPoint(1, 0));
        }
    }

    for (int i = 0; i < directions.size(); i++) {
        QPoint ptNew = item.mPt + directions[i];									//移动后的点
        if (ptNew.x() >= 0 && ptNew.x() <= 8 && ptNew.y() >= 0 && ptNew.y() <= 9)	{	//移动后需仍在棋盘

        }
        else {
            continue;
        }
        CCItem item2;
        if (FindItemAtPoint(ptNew, item2) && item.mRed == item2.mRed) {
            //终点是同色的棋子
            continue;
        }
        moveArea.append(ptNew);
    }
}


bool CCGameWnd::FindItemAtPoint(QPoint pt, CCItem &item)
{
    for (int i = 0; i < mItems.size(); i++) {
        if (mItems[i].mPt == pt) {
            item = mItems[i];
            return true;
        }
    }
    return false;
}

bool CCGameWnd::DeleteItemAtPoint(QPoint pt, bool &bDeleteSHUAI)
{
    bool bDeleted = false;
    for (int i = 0; i < mItems.size(); i++) {
        if (mItems[i].mPt == pt) {
            bDeleteSHUAI = (mItems[i].mType == ITEM_SHUAI);

            mItems.erase(mItems.begin() + i);
            bDeleted = true;
            break;
        }
    }
    return bDeleted;
}

void CCGameWnd::SetItemShow(CCItem item, bool bShow)
{
    for (int i = 0; i < mItems.size(); i++) {
        if (mItems[i].mPt == item.mPt) {
            mItems[i].mShow = bShow;
            break;
        }
    }
}


void CCGameWnd::ChangeItemPoint(QPoint ptItem, QPoint pt)
{
    for (int i = 0; i < mItems.size(); i++) {
        if (mItems[i].mPt == ptItem) {
            mSelectedItem.mPt = QPoint(-1,-1);
            mItems[i].mPt = pt;
            mItems[i].mShow = true;
            break;
        }
    }
}
